package serviceImpl;

import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.stereotype.Service;
import service.CaCheService;
import util.ParameterUtils;
import util.ResponseBean;

import javax.annotation.Resource;

@Service
public class CaCheServiceImpl implements CaCheService {

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private ResponseBean responseBean;

    //Redis存储验证码中Hash的键
    private String codeKey = "phone_code";

    @Override
    public ResponseBean outPhoneCode(String phone, String code) {
        ResponseBean responseBean = new ResponseBean();

        if (code == null) {
            responseBean.setAll(ResponseBean.HTTP_CODE_BAD_REQUEST, ResponseBean.MSG_OPERATION_FAILED_MESSAGE, null);

        } else {


            //后续改回去配置文件试试看
            //设置键序列化器
            redisTemplate.setKeySerializer(new Jackson2JsonRedisSerializer(String.class));

            //设置Hash数据Key序列化器
            redisTemplate.setHashKeySerializer(new Jackson2JsonRedisSerializer(String.class));

            //设置Hash数据Value序列化器
            redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer(String.class));
            try {
                //获取操作Redis List操作对象
                HashOperations<String, String, String> hashOperations = redisTemplate.opsForHash();
                //号码为键 验证码为
                hashOperations.put(codeKey, phone, code);
                responseBean.setAll(ResponseBean.HTTP_CODE_OK, ResponseBean.MSG_SUCCESSFUL_OPERATION_MESSAGE, null);

            }
            //不知有啥异常，先来个祖先异常镇压
            catch (Exception e) {
                e.printStackTrace();
                responseBean.setAll(ResponseBean.HTTP_CODE_SYSTEM_ERROR, (ResponseBean.MSG_OPERATION_FAILED_MESSAGE), null);

            }
        }
        return responseBean;
    }


    @Override
    public String putPhoneCode(String phone) {
        String code = null;
        ResponseBean responseBean = new ResponseBean();
        //后续改回去配置文件试试看
        //设置键序列化器
        redisTemplate.setKeySerializer(new Jackson2JsonRedisSerializer(String.class));

        //设置Hash数据Key序列化器
        redisTemplate.setHashKeySerializer(new Jackson2JsonRedisSerializer(String.class));

        //设置Hash数据Value序列化器
        redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer(String.class));

        try {
            HashOperations<String, String, String> hashOperations = redisTemplate.opsForHash();

            code = hashOperations.get(codeKey, phone);

        }
        //不知有啥异常，先来个祖先异常镇压
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        return code;
    }


    @Override
    public String dletePhoneCode(String phone) {

        //后续改回去配置文件试试看
        //设置键序列化器
        redisTemplate.setKeySerializer(new Jackson2JsonRedisSerializer(String.class));

        //设置Hash数据Key序列化器
        redisTemplate.setHashKeySerializer(new Jackson2JsonRedisSerializer(String.class));

        //设置Hash数据Value序列化器
        redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer(String.class));

        HashOperations<String, String, String> hashOperations = redisTemplate.opsForHash();

        hashOperations.delete(codeKey, phone);

        return "OK";
    }
}
